#pragma once
#include <assert.h>

namespace my_std
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end()const
		{
			return _finish;
		}

		size_t capacity()
		{
			size_t capacity = _end_of_storage - _start;
			if (capacity <= 4)
			{
				return 4;
			}
			return capacity;
		}

		size_t size()
		{
			return _finish - _start;
		}

		vector(size_t n, const T& x)
		{
			resize(n, x);
		}

		vector(int n, const T& x)
		{
			resize(n, x);
		}

		template <class Type>
		vector(Type& frist, Type& last)
		{
			while (frist != last)
			{
				push_back(*frist);
				++frist;
			}
		}

		vector()
		{}

		vector(const vector<T>& v)
		{
			_start = new T[v.size()];
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + v.size();
			_end_of_storage = _finish;
		}


		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		
		void resize(size_t n,const T& v = T())
		{
			reserve(n);
			if (size() > n)
			{
				_finish = _start + n;
			}
			else
			{
				
				while (_finish != _start + n)
				{
					*_finish = v;
					++_finish;
				}
			}
			
		}
		

		void reserve(size_t n)
		{
			if (n >= capacity())
			{
				size_t sz = size();

				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, size() * sizeof(T));
					delete[] _start;
				}
				
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void pop_back()
		{
			erase(end()--);
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator end = pos;
			while (end != _finish)
			{
				end[0] = end[1];
				++end;
			}
			--_finish;

			return pos;
		}
		
		iterator insert(iterator pos,const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			size_t m = pos - _start;
			if (_finish == _end_of_storage)
			{
				reserve(capacity() * 2);
				pos = _start + m;
			}

			iterator end = _finish;

			while (end != pos)
			{
				end[0] = end[-1];
				end--;
			}

			pos[0] = x;
			++_finish;
			return pos;
		}

		T& operator[](size_t pos)
		{
			return _start[pos];
		}

		const T& operator[](const size_t pos)const
		{
			return _start[pos];
		}

		void clear()
		{
			_finish = _start;
		}


		template<class InputIterator, class T>
		InputIterator find(InputIterator first, InputIterator last, const T& val)
		{
			while (first != last) {
				if (*first == val) return first;
				++first;
			}
			return last;
		}
				

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;


	};


	
	

};